package com.shuoyue.ycgk.ui.recruitment.resume.major;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.views.dialog.CustomPicker;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectMajorActivityTab extends BaseMvpActivity {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.edu)
    TextView edu;
    @BindView(R.id.major)
    EditText major;
    @BindView(R.id.search)
    Button search;
    @BindView(R.id.backlist)
    TextView backlist;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    IndexFragmentStateAdapter indexFragmentStateAdapter;
    List<BaseMvpFragment> fragmentMainItems;


    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_major_tab;
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initView() {
        pageTitle.setText("选择专业");
        fragmentMainItems = new ArrayList<>();
        fragmentMainItems.add(FragmentMajorList.getInstance("专科"));
        fragmentMainItems.add(FragmentMajorList.getInstance("本科"));
        fragmentMainItems.add(FragmentMajorList.getInstance("研究生"));
        fragmentMainItems.add(FragmentMajorList.getInstance("博士"));

        indexFragmentStateAdapter = new IndexFragmentStateAdapter(getSupportFragmentManager(), fragmentMainItems);
        tabLayout.setupWithViewPager(viewpager);
        viewpager.setOffscreenPageLimit(1);
        viewpager.setAdapter(indexFragmentStateAdapter);
        viewpager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        edu.setText("专科");
                        break;
                    case 1:
                        edu.setText("本科");

                        break;
                    case 2:
                        edu.setText("研究生");

                        break;
                    case 3:
                        edu.setText("博士");

                        break;
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.edu, R.id.search, R.id.backlist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edu:
                showeducation();
                break;
            case R.id.search:
                FragmentMajorList fragmentMajorList = (FragmentMajorList) indexFragmentStateAdapter.getCurrentFragment();
                fragmentMajorList.search(major.getText().toString());
                break;

        }
    }


    /**
     * 学历
     */
    void showeducation() {
        String[] dataArray = new String[]{"专科", "本科", "研究生", "博士"};
        CustomPicker picker = new CustomPicker(this, dataArray, "学历");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            edu.setText(option);
            for (int i = 0; i < fragmentMainItems.size(); i++) {
                BaseMvpFragment f = fragmentMainItems.get(i);
                if (f.getTitle().equals(option)) {
                    viewpager.setCurrentItem(i);
                }
            }
        });
        picker.show();
    }
}
